package linear_list.doublelinkedlist;

/***
 * 2、无头双向链表实现
 */
public class DoubleLinkedList {
    private int size = 0;
    private Node head = null;

    //头插法
    public void addFirst(int data){addIndex(0,data);}
    //尾插法
    public void addLast(int data){addIndex(size,data);}
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        if(index < 0 || index > size){
            System.err.println("input error");
            return false;
        }
        Node newNode = new Node(data);
        Node pHead = head;
        while(index > 1){
            pHead = pHead.next;
            index--;
        }
        if(head == null){
            head = newNode;
        }else if(index == 0){
            newNode.next = head;
            head.prev = newNode;
            head = newNode;
        }else{
            newNode.prev = pHead;
            newNode.next = pHead.next;
            pHead.next = newNode;
        }
        size++;
        return true;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        Node pHead = head;
        while(pHead != null){
            if(pHead.data == key)
                return true;
            pHead = pHead.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head.data == key){
            Node temp = head;
            head = head.next;
            temp.next = null;
        }
        else{
            Node pHead = head;
            while(pHead.next != null){
                if(pHead.next.data == key){
                    Node temp = pHead.next;
                    //先判断下下个元素是否为null，再删除节点，否则会空指针异常
                    if(pHead.next.next != null){
                        pHead.next.prev = pHead;
                    }
                    pHead.next = pHead.next.next;
                    temp.next = null;
                    return;
                }
                pHead = pHead.next;
            }
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        while(head.data == key){
            Node temp = head;
            head = head.next;
            head.prev = null;
            temp.next = null;
            temp.prev = null;
        }
        if(head != null){
            Node pHead = head.next;
            while(pHead.next != null){
                if(pHead.next.data == key){
                    Node temp = pHead.next;
                    if(pHead.next.next != null) {
                        pHead.next.prev = pHead;
                    }
                    pHead.next = pHead.next.next;
                    temp.next = null;
                    temp.prev = null;
                }
                else {
                    pHead = pHead.next;
                }
            }
        }
    }
    //得到单链表的长度
    public int size(){
        return size;
    }
    public int dataBeforeIndex(int index){
        Node pHead = head;
        while(index > 0){
            pHead = pHead.next;
            index--;
        }
        return pHead.prev.data;
    }
//    public void display();
    public void clear(){
        clearHelper(head);
        head = null;
    }

    private void clearHelper(Node head) {
        if(head == null)
            return;
        clearHelper(head.next);
        head.prev = null;
        head.next = null;
    }

    public String toString(){
        String str = "";
        Node pHead = head;
        while(pHead != null){
            str += pHead.data + "->";
            pHead = pHead.next;
        }
        str += "NULL";
        return str;
    }
}
class Node{
    public int data;
    public Node prev;
    public Node next;
    public Node(int data){
        this.data = data;
    }
}
